package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysMenu;
import com.itheima.youdianda.dto.SysMenuDto;
import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.mapper.SysMenuMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* @author YUAN
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2022-09-06 18:36:38
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Override
    public List<SysMenuDto> getAllMenus() {
        return getChildren(0);
    }

    private List<SysMenuDto> getChildren(int pid){
        List<SysMenuDto> childrenMenus = baseMapper.getChildren(pid);
        if(childrenMenus == null){
            return null;
        }
        for (SysMenuDto childrenMenu : childrenMenus) {
            childrenMenu.setChildren(getChildren(childrenMenu.getId()));
        }
        childrenMenus.sort(Comparator.comparingInt(SysMenu::getSort));
        return childrenMenus;
    }
}




